import Vue from 'vue'
import VueRouter from 'vue-router'
// import LoginCom from '../components/LoginCom'
// import WorkBenchCom from '../components/WorkBenchCom'
// import WorkNavCom from '../components/WorkNavCom'

import NotFoundCom from '../components/NotFoundCom'
// 配置中心路由
import baseRoutes from '../components/base/routes'
//CRM路由
import crmRoutes from '../components/crm/routes'
import bxRoutes from '../components/bx/routes'
//欢迎界面
import welcomeRoutes from '../components/worknav/routes'
//营销预计
import annualRoutes from '../components/salePredict/routes'
//保险路由
import insuranceRoutes from '../components/insurance/routes'
//供应链路由
import scmRoutes from '../components/scm/routes'

import testRoutes from '../components/test/routes'
// 启用路由
Vue.use(VueRouter)

//404路由
const notFoundRoutes = [{
    path: '*',
    component: NotFoundCom
}]


// 登录窗口
const LoginCom = resolve => {
    require.ensure(['../components/LoginCom'], () => {
        resolve(require('../components/LoginCom'))
    })
}
const WorkBenchCom = resolve => {
    require.ensure(['../components/WorkBenchCom'], () => {
        resolve(require('../components/WorkBenchCom'))
    })
}
const WorkNavCom = resolve => {
    require.ensure(['../components/WorkNavCom'], () => {
        resolve(require('../components/WorkNavCom'))
    })
}

//登录路由
const loginRoutes = [{
    path: '/login',
    name: 'login',
    component: LoginCom
}];

//主路由
const mainRoutes = [{
    path: '/main',
    component: WorkBenchCom,
    children: [{
        // 当 /user/:id/profile 匹配成功，
        // UserProfile 会被渲染在 User 的 <router-view> 中
        path: '',
        ailas: '',
        component: WorkNavCom
    }].concat(baseRoutes).concat(testRoutes).concat(crmRoutes).concat(bxRoutes).concat(scmRoutes).concat(insuranceRoutes).concat(welcomeRoutes).concat(annualRoutes).concat(notFoundRoutes)
}];

//合并路由
const routes = loginRoutes.concat(mainRoutes).concat(notFoundRoutes);

const csRouter = new VueRouter({
        mode: 'history',
        routes
    })
    //全局导航拦截
csRouter.beforeEach((to, from, next) => {
    if (to.path != '/login') {
        var token = window.localStorage.getItem("token");
        if (token) {
            next(true);
        } else {
            next('/login');
            return;
        }
    } else {
        next(true);
    }
})

export default csRouter